package com.jy.framework.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.cglib.beans.BeanMap;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @Author: JuiceYan
 * @Description: 控制器查询结果的包装类基类
 * @Class: BaseControllerWarpper
 * @Date: 22:01 2018/12/5
 */
public class BaseControllerWarpper {

    public static <T> Object warp(T obj, Consumer<? super Map<String, Object>> wrap) {
        if (obj instanceof List) {
            List<Map<String, Object>> list = (List<Map<String, Object>>) obj;
            for (Map<String, Object> map : list) {
                wrap.accept(map);
            }
            return list;
        } else if (obj instanceof Map) {
            Map<String, Object> map = (Map<String, Object>) obj;
            wrap.accept(map);
            return map;
        } else if (obj instanceof IPage) {
            IPage page = (IPage) obj;
            for (Object map : page.getRecords()) {
                if (map instanceof Map) {
                    Map<String, Object> map1 = (Map<String, Object>) map;
                    wrap.accept(map1);
                } else {
                    Map<String, Object> map1 = BeanMap.create(map);
                    wrap.accept(map1);
                }
            }
            return page;
        } else {
            return obj;
        }
    }

}
